package com.geek.spring.bucks.service;


import com.geek.spring.bucks.cachedao.CoffeeCacheRepository;
import com.geek.spring.bucks.mapper.CoffeeMapper;
import com.geek.spring.bucks.model.Coffee;
import com.geek.spring.bucks.model.CoffeeExample;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;

import org.jadira.usertype.spi.utils.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.exact;

@Slf4j
@Service
public class CoffeeService {
    @Resource
    private CoffeeMapper coffeeMapper;

//    @Autowired
//    private JedisPool jedisPool;

    @Resource
    CoffeeCacheRepository cacheRepository;


    public Coffee findById(Long id) {
        Coffee coffee = coffeeMapper.selectByPrimaryKey(id);
        cacheRepository.save(coffee);
        return coffee;
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertCoffee(Coffee coffee) {
        log.info("內部");
        log.info(coffeeMapper.selectAllCount().toString());
        Coffee latte = new Coffee()
                .withName(coffee.getName())
                .withPrice(coffee.getPrice())
                .withCreateTime(new Date())
                .withUpdateTime(new Date());
        coffeeMapper.insert(latte);
        log.info("內部");
//        log.info(coffeeMapper.selectAllCount().toString());
//        int i = 1/0;


    }


    @Transactional(rollbackFor = Exception.class)
    public void updateCoffee(Coffee coffee) {
        Coffee latte = new Coffee()
                .withId(coffee.getId())
                .withName(coffee.getName())
                .withPrice(coffee.getPrice())
                .withCreateTime(new Date())
                .withUpdateTime(new Date());
        CoffeeExample example = new CoffeeExample();
        example.createCriteria().andIdEqualTo(coffee.getId());
        coffeeMapper.updateByExample(latte, example);
//        log.info(coffeeMapper.selectAllCount().toString());
//        int i = 1 / 0;
    }

    public Long selectCount() {
        long count = coffeeMapper.selectAllCount();
        return count;
    }

    public PageInfo<Coffee> selectPage(int index, int page, Coffee query) {
        CoffeeExample queryExample = new CoffeeExample();
        if (StringUtils.isNotEmpty(query.getName())) {
            queryExample.createCriteria().andNameEqualTo(query.getName());
        }


        List<Coffee> coffees = coffeeMapper.selectByExampleWithRowbounds(queryExample, new RowBounds(index, page));
        PageInfo<Coffee> pageCoffee = new PageInfo(coffees);
        return pageCoffee;
    }


}
